<template>
  <avue-crud :class="type=='view'?'top15':''" :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form" :permission="permissionList" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave" :before-open="beforeOpen" @selection-change="selectionChange" @refresh-change="refreshChange" @on-load="onLoad">
    <template slot-scope="{row}" slot="runningStatus">
      <img :src="row.runningStatus=='1'?greenImg:row.runningStatus=='2'?yellowImg:row.runningStatus=='3'?redImg:''" class="statusImg" />
    </template>
    <template slot-scope="{row}" slot="menu">
      <el-button type="text" size="small" icon="el-icon-delete" @click.stop="rowDel(row, row.$index)">删除</el-button>
    </template>
  </avue-crud>
</template>

<script>
import { numparZReg, numParseReg, codeReg } from '@/util/zhengze';
import { init } from '@/mixins/init';

export default {
  mixins: [init],
  props: {
    status: {
      type: String,
    },
    projectNo: {
      type: String,
    },
    projectName: {
      type: String,
    },
    prodPlanNo: {
      type: String,
    },
    currentPlanId: {
      type: String,
    },
    activeName: {
      type: String,
    },
    productDrawingNo: {
      type: String,
    },
    productName: {
      type: String,
    },
    planStartEndDate: {
      type: Array,
    },
  },
  data() {
    return {
      fileTitle: 'PO订单生产计划',
      greenImg: require('@/styles/images/green.png'), //正常
      yellowImg: require('@/styles/images/yellow.png'), //预警
      redImg: require('@/styles/images/red.png'), //超期
      option: {
        header: false,
        addRowBtn: false,
        cellBtn: false,
        delBtn: false,
        cancelBtn: false,
        viewBtn: false,
        column: [
          {
            label: '客户名称',
            prop: 'customerName',
            overHidden: true,
          },
          {
            label: 'PO订单号',
            prop: 'jobOrder',
            overHidden: true,
          },
          {
            label: '订购数量',
            prop: 'num',
            overHidden: true,
          },
          {
            label: '生产数量',
            prop: 'purchaseNum',
            minWidth: 120,
            type: 'number',
            minRows: 0,
            maxRows: 99999,
            precision: 0,
            cell: true,
          },
          {
            label: '交货日期',
            prop: 'deliveryDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            disabled: true,
            minWidth: 120,
            rules: [
              {
                required: true,
                message: '请选择交货日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划开始日期',
            prop: 'planStartDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            rules: [
              {
                required: true,
                message: '请选择计划开始日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划结束日期',
            prop: 'planEndDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            rules: [
              {
                required: true,
                message: '请选择计划结束日期',
                trigger: 'blur',
              },
            ],
          },

          {
            label: '实际开始日期',
            prop: 'actualStartDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
          {
            label: '实际完成日期',
            prop: 'actualEndDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
        ],
      },
      data: [],
      customerList: [],
    };
  },
  watch: {},
  computed: {},
  created() {
    if (this.type == 'edit') {
      this.option.header = true;
      this.option.menu = true;
    } else {
      this.option.header = false;
      this.option.menu = false;
    }
    this.findObject(this.option.column, 'planStartDate').pickerOptions.disabledDate = (time) => {
      return (
        time.getTime() < new Date(this.taskStartDate).getTime() - 86400000 ||
        time.getTime() > new Date(this.taskEndDate).getTime()
      );
    };
    this.findObject(this.option.column, 'planEndDate').pickerOptions.disabledDate = (time) => {
      return (
        time.getTime() < new Date(this.taskStartDate).getTime() - 86400000 ||
        time.getTime() > new Date(this.taskEndDate).getTime()
      );
    };
    this.findObject(this.option.column, 'actualStartDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'actualEndDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
  },
  mounted() {},
  methods: {
    addRow() {
      this.$refs.crud.rowCellAdd({
        purchaseNum: 1,
      });
    },
    rowSave(row, done, loading) {
      if (!numParseReg.test(row.purchaseNum)) {
        this.$message.error('生产数量请输入大于0的整数');
        loading();
        return;
      }
      if (row.planStartDate && row.planEndDate && new Date(row.planStartDate) > new Date(row.planEndDate)) {
        this.$message.error('计划开始日期不应晚于计划结束日期');
        loading();
        return;
      }
      if (row.actualStartDate && row.actualEndDate && new Date(row.actualStartDate) > new Date(row.actualEndDate)) {
        this.$message.error('实际开始日期不应晚于实际结束日期');
        loading();
        return;
      }
      this.data.push(row);
      done();
    },
    addUpdate(row, index, done, loading) {
      if (!numParseReg.test(row.purchaseNum)) {
        this.$message.error('生产数量请输入大于0的整数');
        loading();
        return;
      }
      if (row.planStartDate && row.planEndDate && new Date(row.planStartDate) > new Date(row.planEndDate)) {
        this.$message.error('计划开始日期不应晚于计划结束日期');
        loading();
        return;
      }
      if (row.actualStartDate && row.actualEndDate && new Date(row.actualStartDate) > new Date(row.actualEndDate)) {
        this.$message.error('实际开始日期不应晚于实际结束日期');
        loading();
        return;
      }
      this.data.splice(index, 1, row);
      done();
    },
    rowDel(row, index) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.data.splice(index, 1);
      });
    },
  },
};
</script>

<style>
</style>
